package pao.akka.arithmetic;

import akka.actor.ActorSelection;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import pao.akka.util.GBestMsg;
import pao.akka.util.PBestMsg;
import pao.akka.util.PsoValue;

/**
 * @Author: 陶鹏飞
 * @Description:
 * @Date: 2018/9/10-22:18
 * @email: taopengfei1223@foxmail.com
 **/
public class MasterBird extends UntypedActor {

    private final LoggingAdapter loggingAdapter = Logging.getLogger(getContext().system(),this);
    private PsoValue gBest = null;

    @Override
    public void onReceive(Object o) throws Exception {
        if (o instanceof PBestMsg) {
            PsoValue pBest = ((PBestMsg) o).getPsoValue();
            if (gBest == null || gBest.getValue() < pBest.getValue()) {
                //更新全局最优，通知所有粒子
                System.out.println(o + "\n");
                gBest = pBest;
                ActorSelection actorSelection = getContext().actorSelection("/user/bird_*");
                actorSelection.tell(new GBestMsg(gBest),getSelf());
            }
        } else {
            unhandled(o);
        }
    }
}
